<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="公告标题" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入公告标题" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="显示范围" prop="reader">
        <el-select v-model="queryParams.reader" placeholder="请选择显示范围" clearable>
          <el-option value="平台" />
          <el-option value="代理" />
          <el-option value="店铺" />
          <el-option value="代运营" />
        </el-select>
      </el-form-item>
      <el-form-item label="有效时间" prop="effect">
        <el-date-picker v-model="queryParams.effect" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="请选择有效时间" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:notice:add']">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="noticeList">
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="标题" align="center" prop="title">
        <template slot-scope="scope">
          <el-link :type="scope.row.color" @click="handleUpdate(scope.row)">{{ scope.row.title }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="显示范围" align="center" prop="reader" />
      <el-table-column label="生效时间" align="center" prop="effect" />
      <el-table-column label="失效时间" align="center" prop="expire" />
      <el-table-column label="发布时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:notice:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:notice:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改公告对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="780px" class="notice-dialog">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="内容" prop="body">
          <editor v-model="form.body" :height="240" />
        </el-form-item>
        <el-form-item label="显示范围" prop="readerList">
          <el-checkbox-group v-model="form.readerList" size="medium">
            <el-checkbox label="平台" />
            <el-checkbox label="代理" />
            <el-checkbox label="店铺" />
            <el-checkbox label="代运营" />
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="置顶级别" prop="rank" required>
          <el-slider :max='9' :step='1' v-model="form.rank"></el-slider>
        </el-form-item>
        <el-form-item label="标题颜色" prop="color">
          <el-select v-model="form.color" :class="form.color" clearable>
            <el-option label="主要" value="primary"><el-link type="primary">主要</el-link></el-option>
            <el-option label="成功" value="success"><el-link type="success">成功</el-link></el-option>
            <el-option label="信息" value="info"><el-link type="info">信息</el-link></el-option>
            <el-option label="警告" value="warning"><el-link type="warning">警告</el-link></el-option>
            <el-option label="危险" value="danger"><el-link type="danger">危险</el-link></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="生效时间" prop="effect">
          <el-date-picker v-model="form.effect" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择生效时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="失效时间" prop="expire">
          <el-date-picker v-model="form.expire" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择失效时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listNotice, getNotice, delNotice, addNotice, updateNotice } from "@/api/system/notice";
import { formatDate } from "@/utils";

export default {
  name: "Notice",
  dicts: ['sys_notice_status', 'sys_notice_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公告表格数据
      noticeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: undefined,
        reader: undefined,
        effect: formatDate(new Date()),
      },
      // 表单参数
      form: {
        title: '',
        body: '',
        readerList: [],
        rank: 0,
        color: '',
        effect: '',
        expire: '',
      },
      // 表单校验
      rules: {
        title: [
          { required: true, message: "公告标题不能为空", trigger: "blur" }
        ],
        body: [
          { required: true, message: "公告内容不能为空", trigger: "blur" }
        ],
        readerList: [
          { required: true, message: '必须选择显示范围', trigger: 'blur' }
        ],
        effect: [
          { required: true, message: '必须选择生效时间', trigger: 'change' }
        ],
        expire: [
          { required: true, message: '必须选择失效时间', trigger: 'change' }
        ],
      }
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    /** 查询公告列表 */
    getList() {
      this.loading = true;
      listNotice(this.queryParams).then(response => {
        this.noticeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      const now = new Date()
      this.form = {
        id: undefined,
        title: '',
        body: '',
        readerList: ['平台', '代理', '店铺', '代运营'],
        color: '',
        rank: 0,
        effect: formatDate(now.setHours(0, 0, 0, 0)),
        expire: formatDate(now.setHours(23, 59, 59, 0) + 30 * 86400 * 1000),
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加公告";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getNotice(row.id).then(response => {
        this.form = response.data
        this.open = true;
        this.title = "修改公告";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateNotice(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNotice(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除公告编号为"' + row.id + '"的数据项？').then(function () {
        return delNotice(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    }
  }
};
</script>
<style lang="scss">
.notice-dialog {
  .el-select {
    &.primary input {
      color: #46a6ff;
    }

    &.success input {
      color: #13ce66;
    }

    &.info input {
      color: #909399;
    }

    &.warning input {
      color: #ffc833;
    }

    &.danger input {
      color: #ff4949;
    }
  }
}
</style>
